package com.hospital.pharmacy.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.pharmacy.entity.SalesRecord;
import com.hospital.pharmacy.service.SaleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: Hospital
 * @description: 销售controller
 * @author: 杨曼珍
 * @create: 2019-01-08 11:47
 **/
@Controller
@RequestMapping(value = "/saleRecord")
public class SaleRecordController {
    @Autowired
    SaleRecordService saleRecordService;

    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public String getAllSale(){
        return saleRecordService.getAllSale();
    }

    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String insertSaleRecord(@RequestBody String data)throws ParseException {

        JSONObject jsonObject=JSON.parseObject(data);
        String mdcNum=jsonObject.getString("mdcNum");
        String salRecCount=jsonObject.getString("salRecCount");
        String salRecPrice=jsonObject.getString("salRecPrice");
        String salRecTime=jsonObject.getString("salRecTime");
        String salRecTotal=jsonObject.getString("salRecTotal");
        System.out.println(data);
        return saleRecordService.saleMed(Integer.parseInt(mdcNum),new SimpleDateFormat("yyyy-MM-dd").parse(salRecTime),Integer.parseInt(salRecCount),Double.parseDouble(salRecPrice),Double.parseDouble(salRecTotal),"新的售货");
    }


    @RequestMapping(value = "/mdcName",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String selectSaleByMedName(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String mdcName=jsonObject.getString("mdcName");
        return saleRecordService.selectSaleByMedName(mdcName);
    }

    @RequestMapping(value = "/date",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String selectSaleByDate(@RequestBody String data)throws ParseException{
        JSONObject jsonObject=JSON.parseObject(data);
        String salRecTime=jsonObject.getString("salRecTime");
        return saleRecordService.selectSaleByDate(new SimpleDateFormat("yyyy-MM-dd").parse(salRecTime));
    }

    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody String data)throws ParseException{
        JSONObject jsonObject=JSON.parseObject(data);
        Integer salId=jsonObject.getInteger("salId");
        Integer salRecCount=jsonObject.getInteger("salRecCount");
        Double salRecPrice=jsonObject.getDouble("salRecPrice");
        String salRecTime=jsonObject.getString("salRecTime");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(salRecTime);
        Double salRecToTal=jsonObject.getDouble("salRecToTal");
        return saleRecordService.update(salId,salRecCount,salRecPrice,date,salRecToTal);
    }

    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String salId=jsonObject.getString("salId");
        return saleRecordService.delete(Integer.parseInt(salId));
    }

}
